﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common.Utils
{
    public class TimeStampHelper
    {
        /// <summary>  
        /// 获取时间戳Timestamp    
        /// </summary>  
        /// <param name="dt"></param>  
        /// <returns></returns>  
        public static long GetTimeStamp(DateTime dt)
        {
            var dateStart = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1), TimeZoneInfo.Local);
            var timeStamp = (dt.ToUniversalTime().Ticks - dateStart.Ticks) / 10000;
            GetDateTime(timeStamp);
            return timeStamp;
        }

        /// <summary>  
        /// 时间戳Timestamp转换成日期  
        /// </summary>  
        /// <param name="timeStamp"></param>  
        /// <returns></returns>  
        public static DateTime GetDateTime(long timeStamp)
        {
            var dtStart = new DateTime(1970, 1, 1);
            var timeSpan = new TimeSpan(timeStamp * 10000); 
            var targetDt = dtStart.Add(timeSpan);
            return targetDt.ToLocalTime();
        }
    }
}
